<?php
// +----------------------------------------------------------------------------
// | Tke团队版权所属 [三十年河东三十年河西,莫欺少年穷.!]
// +----------------------------------------------------------------------------
// | Copyright (c) 2014 http://www.itdaodan.com All rights reserved.
// +----------------------------------------------------------------------------
// | Author: Cruise <jiangyu@hrbjjhh.com><jiangyu@hrbjjhh.com>
// +----------------------------------------------------------------------------

namespace Admin\Model;
use Admin\Model\BaseModel;
/**
 * @todo 城市模型
 * @author Cruise <jiangyu@hrbjjhh.com>
 */
class WardModel extends BaseModel {
	
	/* 自动验证规则 */
	protected $_validate = array(
			array('ward_name', 'require', '行政区不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
			//array('province_name', 'require', '请输入省份', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
			array('city_id', 'require', '请选择城市', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
			array('province_id', 'require', '请选择省份', self::MUST_VALIDATE, 'regex', self::MODEL_INSERT),
			array('ward_name', 'checkName', '行政区已经存在', self::MUST_VALIDATE, 'callback', self::MODEL_INSERT),
			array('ward_name', '1,20', '省份长度不能超过20个字符', self::MUST_VALIDATE, 'length', self::MODEL_BOTH),
	);
	
	/* 自动完成规则 */
	protected $_auto = array(
			array('create_time', NOW_TIME, self::MODEL_INSERT),
			array('update_time', NOW_TIME, self::MODEL_UPDATE),
			//array('status', '1', self::MODEL_INSERT),
	);
	
	
	/**
	 * @todo 根据行政区id取出行政区名称  
	 * @param int $map_ward_id 行政区id
	 * @param string $field 查询字段
	 * @return false|array 一维数组
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function getWard($map_ward_id = 0,$field = ''){
		//获取数据
		$map_ward_id = (int)$map_ward_id;
		if($map_ward_id == 0){
			$this->error = '请正确操作';
			return false;
		}
		$map = array();//初始化查询条件
		//查询条件
		$map['ward_id'] = $map_ward_id;
		$map['status'] = array('gt',-1);
		//返回值
		return $this->where($map)->field($field)->find();
	}
	
	/**
	 * @todo 检测名称是否存在
	 * @return boolean          ture - 未重名，false - 已重名
	 */
	protected function checkName($ward_name){
		$ward_id = (int)I('ward_id');
		$city_id = (int)I('city_id');
		//判断是否存在
		$map = array();
		if($ward_name != 0){
			$map['ward_id'] = array('neq',$ward_id);
		}
		$map['ward_name'] = $ward_name;
		$map['status'] = array('gt',-1);
		$map['city_id'] = $city_id;
		if($this->where($map)->find()){
			return false;
		}
		return true;
	}
	
	/**
	 * @todo 行政区详情
	 * @param int $ward_id 行政区id
	 * @param string $field 查询字段
	 * @return false|array
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function AreaShow($ward_id = 0,$field = ''){
		//获取数据
		$ward_id = (int)$ward_id;
		if($ward_id == 0){
			$this->error = '请正确操作';
			return false;
		}
		$map = array();//初始化查询条件
		//查询条件
		$map['ward_id'] = $ward_id;
		$map['status'] = array('gt',-1);
		//返回值
		return $this->where($map)->field($field)->find();
	}
	 
	/**
	 * @todo 根据省份id取出城市名称
	 * @param int $province_id 省份id
	 * @param string $field 查询字段
	 * @return false|array
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function getWardName($city_id = 0,$field = 'ward_name'){
		//获取数据
		$city_id = (int)$city_id;
		if($city_id == 0){
			$this->error = '请正确操作';
			return false;
		}
		$map = array();//初始化查询条件
		//查询条件
		$map['city_id'] = $city_id;
		$map['status'] = array('gt',-1);
		//返回值
		return $this->where($map)->field($field)->select();
	}
	
	/**
	 * @todo 删除城市验证，调取行政区列表
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function getWardList($map = array(),$field='',$orderby='ward_id desc'){
		return $this->where($map)->field($field)->order($orderby)->select();
	}
	
	/**
	 * @todo 判断城市下是否有行政区数据
	 * @param string $city_id
	 * @author Cruise <jiangyu@hrbjjhh.com>
	 */
	public function judgeCity($city_id=''){
		if($city_id == ''){
			$this->error='参数丢失，请重试';
			return false;
		}
		$map['city_id'] = array('in',$city_id);
		$map['status'] = array('gt',-1);
		$ward_list = $this->getWardList($map,'ward_id');
		if(!empty($ward_list)){
			$this->error = '城市下有行政区，请先删除行政区';
			return false;
		}
		return true;
	}
	
	/**
	 * @todo 返回主键
	 * @see \Think\Model::getPk()
	 * @author PHP@妖孽 <mashengjie@hrbjjhh.com>
	 */
	public function getPk(){
		return 'ward_id';
	}
}
